<template>
	<view class="container">
		<view class="list">
			<view class="item" v-for="(item, index) in list" :key="index" @click="selectAddress(item)">
				<view class="left">
					<view class="city">
						<text v-if="item.default == '1'">默认</text>
						<view class="text">
							{{ item.formatted_address }}
						</view>
					</view>
					<view class="info">
						{{ item.address }}
					</view>
					<view class="user">
						{{ item.name }} {{ item.mobile }}
					</view>
				</view>
				<image :src="$img_path('/pagesMine/65.png')" @click.stop="toForm(2, item)" mode="widthFix"></image>
			</view>
		</view>
		<view class="add-btn">
			<button @click="toForm(1)">新增地址</button>
		</view>
	</view>
</template>

<script>
import { getAddressList } from '@/utils/api/serviceApi';
	export default {
		data() {
			return {
				page: {
					page: 1
				},
				pageEnd: false,
				list: []
			};
		},
		onLoad() {
			this.getList()
		},
		onReachBottom() {
			this.getList()
		},
		methods:{
			toForm(type, address){
				const that = this
				uni.navigateTo({
					url:`/pagesService/address/address_form?type=${type}&address=${encodeURIComponent(JSON.stringify(address))}`,
					success:function(){
						uni.$once("refreshAddressList", () => {
							that.pageEnd = false
							that.page.page = 1
							that.getList()
						})
					}
				})
			},
			async getList() {
				if (this.pageEnd) {
					return
				}
				try{
					const res = await getAddressList({
						page: this.page.page
					})
					if (this.page.page == 1) {
						this.list = res.data.data
					} else {
						this.list = this.list.concat(res.data.data)
					}
					if (res.data.data.length <= 0) {
						this.pageEnd = true
					} else {
						this.page.page += 1
					}
					console.log("this.list", this.list)
				}catch(e){
					//TODO handle the exception
					console.log("获取地址列表失败", e)
				}
			},
			selectAddress(address) {
				console.log("address", address)
				uni.$emit("updateServiceOrderAddress", {
					data: address
				})
				uni.navigateBack()
			}
		}
	}
</script>

<style lang="scss" scoped>
.container{
	padding-bottom: calc(constant(safe-area-inset-bottom) + 140rpx);
	padding-bottom: calc(env(safe-area-inset-bottom) + 140rpx);
	.list{
		padding: 0 22rpx;
		box-sizing: border-box;
		.item{
			background: #FFFFFF;
			box-shadow: 0rpx 0rpx 12rpx 2rpx rgba(223,223,223,0.16);
			border-radius: 20rpx;
			padding: 20rpx 30rpx 32rpx 20rpx;
			box-sizing: border-box;
			display: flex;
			align-items: center;
			justify-content: space-between;
			margin-top: 20rpx;
			.left{
				width: calc(100% - 70rpx);
				.city{
					display: flex;
					text{
						display: inline-block;
						width: 92rpx;
						height: 40rpx;
						background: #E72A2A;
						border-radius: 8rpx;
						font-weight: 400;
						font-size: 24rpx;
						color: #FFFFFF;
						line-height: 40rpx;
						text-align: center;
						margin-right: 20rpx;
					}
					.text{
						font-weight: bold;
						font-size: 28rpx;
						color: #000000;
						line-height: 40rpx;
					}
				}
				.info{
					font-weight: 400;
					font-size: 28rpx;
					color: #727272;
					line-height: 40rpx;
					margin-top: 12rpx;
				}
				.user{
					font-weight: bold;
					font-size: 28rpx;
					color: #333333;
					line-height: 40rpx;
				}
			}
			&>image{
				width: 45rpx;
				height: 45rpx;
			}
		}
	}
	
	.add-btn{
		position: fixed;
		bottom: 0;
		left: 0;
		right: 0;
		padding: 20rpx 22rpx;
		padding-bottom: calc(constant(safe-area-inset-bottom) + 20rpx);
		padding-bottom: calc(env(safe-area-inset-bottom) + 20rpx);
		box-sizing: border-box;
		button{
			width: 100%;
			height: 96rpx;
			background: linear-gradient( 135deg, #FFA233 0%, #FB4C4C 100%);
			border-radius: 80rpx;
			font-weight: bold;
			font-size: 36rpx;
			color: #FFFFFF;
			line-height: 96rpx;
		}
	}
}
</style>
